<?php
/**
 *
 * @package    	Digitalus\Ui
 * @copyright  	Copyright (c) 2009,  Digitalus Media USA (http://digitalusmedia.net)
 * @license    	New BSD License, library/Dig/License.txt
 * @version 	2.0
 * 
 */
class DigUi_View_Helper_DigUiBlock extends Zend_View_Helper_Abstract 
{
    /**
     * the block helper wraps the standard view action helper
     * and enables you to call an action using the standard ZF URI convention
     * {module}/{controller}/{action}...
     * 
     * it also:
     * -> disables the layout
     * -> handles caching
     *
     * you can globally refresh the view cache by setting the view property refreshCache to true
     *
     *
     * @param string $url
     * @param array $params
     * @param bool $cacheEnabled, set to false to disable caching
     * @param bool $refreshCache, set to true to refresh the cache
     *
     * @return XHTML
     */
    public function digUiBlock($url, $params = array(), $cacheEnabled = true, $refreshCache = false)
    {
        if($this->view->refreshCache == true) {
            $refreshCache = true;
        }
        if(isset($this->view->cacheEnabled)) {
            $cacheEnabled = $this->view->cacheEnabled;
        }
        if($cacheEnabled) {
            $cache = Dig_Service_Core::getInstance()->getCache();
            // create the cache key
            $cacheKey = md5('dig_block_' . $url . serialize($params));
            if(!$refreshCache) {
                $cachedBlock = $cache->load($cacheKey);
                if($cachedBlock) {
                    return $cachedBlock;
                }
            }
        }
        
        $params['dig_url'] = $url;

        $request = DigNavigation_Service_Url::parse($url, $params)->request;
        $request->setParam('disable_layout', true);
        $response = $this->view->action(
            $request->getActionName(),
            $request->getControllerName(),
            $request->getModuleName(),
            $request->getParams());
        if(!empty($response)) {
            if($cacheEnabled) {
                $cache->save($response, $cacheKey);
            }
            return $response;
        }
    }
}
?>